//
//  ReweetedStatusView.swift
//  SinaWeibo
//
//  Created by 张子国 on 15/11/17.
//  Copyright © 2015年 张子国. All rights reserved.
//

import UIKit
import SnapKit

class ReweetedStatusView: UIView {
    //记录底部约束
    var bottomConstraints : Constraint?
    //数据模型
    var reweetedStatus : Status? {
        didSet{
        reweetedLabel.text = reweetedStatus?.text
            
        //移除底部约束
        self.bottomConstraints?.uninstall()
            
        if let picurls = reweetedStatus?.imageURLs where picurls.count > 0{
        //设置数据源
        pictureView.imageUrls = picurls
        //显示配图
            self.pictureView.hidden = false
            self.snp_updateConstraints(closure: { (make) -> Void in
                //将约束条件转换成 约束类型
                self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
            })
        } else {
            pictureView.hidden = true
            //将约束条件转换成 约束类型
            self.snp_updateConstraints(closure: { (make) -> Void in
                //将约束条件转换成 约束类型
                self.bottomConstraints = make.bottom.equalTo(reweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
            })

            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.8, alpha: 1)
        setupUI()
      
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: 设置UI
    
    private func setupUI() {
        addSubview(reweetedLabel)
        addSubview(pictureView)
        //设置约束
        reweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(reweetedLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(reweetedLabel.snp_left)
            make.width.equalTo(100)
            make.height.equalTo(100)
        }
        
        //转发微博的底部约束
        self.snp_makeConstraints { (make) -> Void in
       self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
    }
    //MARK: 懒加载控件
    private lazy var reweetedLabel : UILabel = UILabel(title: "我是转发微博", size: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    private lazy var pictureView : StatusPictureView = StatusPictureView()

}
